<!Doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="csrf-token" content="{{ csrf_token() }}">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">	
	<link rel="shortcut icon" type="image/x-icon" href="{{URL::asset('handan/images/logo-icon.png')}}" />
	<title>修改密码</title>
	<link rel="stylesheet" href="{{URL::asset('mobile/css/public.css') }}"/>
	<link rel="stylesheet" href="{{URL::asset('mobile/css/modify.css') }}"/>
	<script src="{{URL::asset('mobile/js/jquery.min.js') }}"></script>
	<script src="{{URL::asset('mobile/js/public.js') }}"></script>
</head>
<body>
<div class="content">
	<div>
		<span>手&nbsp; 机  &nbsp;号</span>
		<input type="tel" value="{{$userinfo->tel}}" disabled="disabled" id="moblie"/>
	</div>
	<div>
		<span>验&nbsp; 证  &nbsp;码</span>
		<input type="number" id="code" />
		<a href="javascript:;" class="miao" onclick="settime(this)" style="background: #3290d2;">
			发送验证码
		</a>
	</div>
	<div>
		<span>新&nbsp; 密  &nbsp;码</span>
		<input type="password" id="j_newpwd"/>
	</div>
	<div>
		<span>确认密码</span>
		<input type="password" id="j_affirm_pwd"/>
	</div>
</div>	
<div class="button" onclick="confirm()">
	确认
</div>
@include('mobile.msg')
<script>
var sendIdentify    = "{{ url('/sendIdentify')    }}";
var gotoregister    = "{{ url('/mregister')       }}";
var checkverifycode = "{{ url('/checkverifycode') }}";
var uppwdbytel 		= "{{ url('/uppwdbytel') }}";
var verifyStatus 	= 0;
var t				= 0;
//确认按钮
function confirm(){
	$('.button').removeAttr("onclick");
	yZm();
	if(verifyStatus == 1){
		var newPassWord = $("#j_newpwd").val();
	    var reNewPassWord = $("#j_affirm_pwd").val();
	    if (newPassWord.length < 8 || newPassWord.length > 16) {
	        msg('密码长度为8-16位');
	        $('.button').attr("onclick",'confirm()');
	        return;
	    }
	    if (reNewPassWord.length < 8 || reNewPassWord.length > 16) {
	        msg('密码长度为8-16位');
	        $('.button').attr("onclick",'confirm()');
	        return;
	    }
	    if (newPassWord != reNewPassWord) {
	        msg('两次密码不一致');
	        $('.button').attr("onclick",'confirm()');
	        return;
	    }
	    $.ajax({
	        type: "POST",
	        data: { newPassWord: newPassWord },
	        url: uppwdbytel,
	        dataType: "json",
	        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
	        success:function (d) {
	            if (d.status == 1) {
	                msg('修改成功');
	            } else if (d.status == 2) {
	            	$('.button').attr("onclick",'confirm()');
	                msg("原密码错误!");
	            } else {
	            	$('.button').attr("onclick",'confirm()');
	                msg('修改失败,与原密码一致');
	            }
	        }
	    });	
	}
}
// 判断验证码
function yZm(){
	$('.miao').css({"background":"#a7c8e0;"})
	var usercode=$.trim($('#code').val());
    if(usercode){ //如果有验证码
    	if(usercode.length > 5) {
            $.ajax({
                url:checkverifycode,
                type: 'post',
                data: {code:usercode},
                dataType:'json',
                headers: {'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content') },
                success: function(res){
                    if(parseInt(res.status) == 1){ //正确
                    	$('.button').attr("onclick",'confirm()');
                    	$('.miao').css({"background":"#a7c8e0;"})
                        return verifyStatus=1;
                    }else if(parseInt(res.status) == 0){
                    	$('.button').attr("onclick",'confirm()');
                        msg('请输入正确的验证码');
                        return verifyStatus=0;
                    }
                }
            });
        }else{
        	$('.button').attr("onclick",'confirm()');
            msg('请输入正确的验证码');
            verifyStatus=0;
        }
    }else{ //未填写验证码
    	$('.button').attr("onclick",'confirm()');
        msg('您的验证码为空');
        verifyStatus=0;
    }
}
//发送手机验证码
function settime(obj) {
	var moblie=$.trim($('#moblie').val());
  	$('.miao').removeAttr("onclick");
    $('.miao').css({'disabled':true});
 	$.ajax({
      	url:  sendIdentify,
      	type: 'post',
      	data: {tel:moblie,type:1},
      	dataType:'json',
      	headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
      	success: function(res){
      		if(parseInt(res.status) == 0) { //失败
      			obj.text('发送失败');
               	$(obj).attr('onclick','settime(this)');
      		}else if(parseInt(res.status) == 1){ //成功
             	firsttel = moblie;//记录发送验证码的手机号
             	$(obj).removeAttr('onclick');
             	$(obj).css({'color':'#fff'});
              	$('#code').focus();
              	var countdown=60;
              	var validCode=true;
              	if(validCode){
                  	validCode = false;
                  	clearInterval(t);
                  	t=setInterval(function () {
                      	countdown--;
                      	obj.innerHTML = countdown+"s后重发";
                      	$('.miao').removeAttr("onclick");
                      	$('.miao').css({'disabled':true,'background':'#a7c8e0'});
                      	if (countdown == 0) {
                       		clearInterval(t);
                       		obj.innerHTML="重新发送";
                       		$(obj).css({'color':'#fff'});
                            $('.miao').attr("onclick", 'settime(this)');
                            $('.miao').css({'disabled':false,'background':'#3290d2'});
                       		validCode=true;
                      	}
                 	},1000);
              	}
          	}
      	}
  	});
}
</script>
</body>
</html>	
